package com.qys.livesMall.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.shop.mapper.ShopGoodsSkuStocksMapper;
import com.qys.livesMall.shop.modal.entity.ShopCart;
import com.qys.livesMall.shop.modal.entity.ShopGoodsSkuStocks;
import com.qys.livesMall.shop.service.ShopGoodsSkuStocksService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gmx
 * @since 2023-06-13
 */
@Service
public class ShopGoodsSkuStocksServiceImpl extends ServiceImpl<ShopGoodsSkuStocksMapper, ShopGoodsSkuStocks> implements ShopGoodsSkuStocksService {

    @Resource
    private ShopGoodsSkuStocksMapper skuStocksMapper;

    @Override
    public R decount(List<ShopCart> carts) {
        for (ShopCart cart : carts) {
            Long skuId = cart.getSkuId();
            LambdaQueryWrapper<ShopGoodsSkuStocks> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(ShopGoodsSkuStocks::getSkuId, skuId);
            queryWrapper.eq(ShopGoodsSkuStocks::getState,false);
            queryWrapper.ne(ShopGoodsSkuStocks::getNum, 0);
            queryWrapper.orderByAsc(ShopGoodsSkuStocks::getCreateTime);
            queryWrapper.last("LIMIT 1");
            ShopGoodsSkuStocks skuStocks = skuStocksMapper.selectOne(queryWrapper);
            if (ObjectUtils.isEmpty(skuStocks))return R.fail(MessageUtils.message("understock"));
            //语句级控制，防止超卖
            int count = skuStocksMapper.decount(skuStocks.getId(), cart.getNum());
            if (count <= 0) {
                return R.fail(cart.getName()+":"+MessageUtils.message("understock"));
            }
        }
        return R.ok();
    }
    @Override
    public R decount(Long skuId, Integer num) {
        LambdaQueryWrapper<ShopGoodsSkuStocks> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ShopGoodsSkuStocks::getSkuId, skuId);
        queryWrapper.eq(ShopGoodsSkuStocks::getState,false);
        queryWrapper.ne(ShopGoodsSkuStocks::getNum, 0);
        queryWrapper.orderByAsc(ShopGoodsSkuStocks::getCreateTime);
        queryWrapper.last("LIMIT 1");
        ShopGoodsSkuStocks skuStocks = skuStocksMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(skuStocks)) return R.fail(MessageUtils.message("shop.stocks.null"));
        //语句级控制，防止超卖
        int count = skuStocksMapper.decount(skuStocks.getId(), num);
        if (count <= 0) {
            return R.fail(MessageUtils.message("understock"));
        }
        return R.ok();
    }

    @Override
    public ShopGoodsSkuStocks queryBySkuId(Long skuId) {
        LambdaQueryWrapper<ShopGoodsSkuStocks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopGoodsSkuStocks::getSkuId,skuId);
        queryWrapper.eq(ShopGoodsSkuStocks::getState,false);
        queryWrapper.ne(ShopGoodsSkuStocks::getNum,"0");
        queryWrapper.orderByAsc(ShopGoodsSkuStocks::getCreateTime);
        List<ShopGoodsSkuStocks> list = super.list(queryWrapper);
        if (ObjectUtils.isEmpty(list)){
            return new ShopGoodsSkuStocks();
        }
        return list.get(0);
    }

    @Override
    public Integer totalStockNum(Long shopId) {
        Integer num=skuStocksMapper.totalStockNum(shopId);
        return num==null?0:num;
    }

    @Override
    public Integer totalStockNumOfUse(Long shopId) {
        Integer num=skuStocksMapper.totalStockNumOfUse(shopId);
        return num==null?0:num;
    }


    @Override
    public boolean upStockNum(ShopGoodsSkuStocks stock,Integer oldNum){
        LambdaUpdateWrapper<ShopGoodsSkuStocks> uw=new LambdaUpdateWrapper();
        uw.set(ShopGoodsSkuStocks::getNum,stock.getNum());
        uw.eq(ShopGoodsSkuStocks::getId,stock.getId());
        uw.eq(ShopGoodsSkuStocks::getNum,oldNum);
        return update(uw);
    }

    @Override
    public List<Long> getShopIdByNum(Long skuId, List<Long> shopIds) {
        return skuStocksMapper.getShopIdByNum(skuId,shopIds);
    }

}
